package com.zyq.controller;

import com.zyq.model.common.ResponseBean;
import com.zyq.model.entity.Message;
import com.zyq.model.entity.User;
import com.zyq.service.IMessageService;
import com.zyq.service.IUserService;
import com.zyq.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: zyq
 * @create: 2022-03-02
 **/
@Api( tags = "消息接口")
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private IMessageService messageService;
    @Autowired
    private IUserService userService;
    @Autowired
    private UserUtil userUtil;

    /**
     * 新增message
     */
    @ApiOperation("邀请医生回答")
    @PostMapping("/sendDoctorMessage")
    @RequiresAuthentication
    public ResponseBean add(@RequestBody Message message,@RequestHeader String Authorization)
    {
        message.setAId(userUtil.getUserId());
        messageService.insertMessage(message);
        return new ResponseBean(HttpStatus.OK.value(), "操作成功",null);
    }


    /**
     * 查看我的消息列表
     */
    @ApiOperation("查看我的消息列表")
    @GetMapping("/list")
    @RequiresAuthentication
    public ResponseBean list(@RequestHeader String Authorization)
    {
        Subject subject = SecurityUtils.getSubject();
        // 登录了返回true

        if (subject.isAuthenticated()) {
            Message message = new Message()
                    .setUserId(userUtil.getUserId());
            List<Message> list = messageService.selectMessageList(message);
            for(int i = 0 ; i < list.size() ; i++){
                Message messageTemp = list.get(i);
                String userName = userService.selectById(messageTemp.getAId().intValue()).getUsername();
                if(messageTemp.getDetail().equals("关注")){
                    list.get(i).setDetail(userName+"用户关注了你");
                }
                else if(messageTemp.getDetail().equals("发布")){
                    list.get(i).setDetail("你关注的"+userName+"发布了新文章");
                }
                else if(messageTemp.getDetail().equals("邀请")){
                    list.get(i).setDetail(userName+"用户邀请你回答问题");
                }
                else if(messageTemp.getDetail().equals("点赞")){
                    list.get(i).setDetail(userName+"用户点赞了你的文章");
                }
                else if(messageTemp.getDetail().equals("收藏")){
                    list.get(i).setDetail(userName+"用户收藏了你的文章");
                }
            }
            return new ResponseBean(HttpStatus.OK.value(), "操作成功",list);
        }
        return new ResponseBean(HttpStatus.OK.value(), "暂未登录", null);

    }

    /**
     * 修改message状态
     */
    @ApiOperation("修改消息状态")
    @PutMapping("/{ids}")
    public ResponseBean updateStatus(@PathVariable Long[] ids)
    {
        messageService.updateStatus(ids);
        return new ResponseBean(HttpStatus.OK.value(), "操作成功",null);
    }

    /**
     * 修改message状态
     */
    @ApiOperation("修改消息状态")
    @GetMapping("/updateStatus")
    @RequiresAuthentication
    public ResponseBean updateStatusAll()
    {
        messageService.updateStatusAll(userUtil.getUserId());
        return new ResponseBean(HttpStatus.OK.value(), "操作成功",null);
    }
}
